﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using DataTransferObject;

namespace BusinessLogicLayer
{
    public class TrieuChungBanDauBLL:BLLBase
    {
        private readonly Ibn_bn_TrieuChungBanDauDAL _dal;

        public TrieuChungBanDauBLL(string connectionString = "")
            : base(connectionString)
        {
            this._dal = new bn_bn_TrieuChungBanDauDAL(this.DatabaseFactory);
        }

        public IQueryable<TrieuChungBanDauDto> Get(int facilityId, string key = "")
        {
            var query = this._dal.GetAll()
                .Where(e => e.IdBenhNhan == facilityId)
                .Select(e => new TrieuChungBanDauDto
                {
                    Id = e.Id,
                    Date = e.Date,
                    IdBenhNhan=e.IdBenhNhan,
                    IdTrieuChung=e.IdTrieuChung,
                    GiaTri=e.GiaTri
                });

            /*if (!string.IsNullOrEmpty(key))
            {
                query = query.Where(e => e.Ten.StartsWith(key));
            }*/
            return query;
        }

        public IQueryable<TrieuChungBanDauDto> GetAll()
        {
            var query = this._dal.GetAll()
                .Select(e => new TrieuChungBanDauDto
                {
                    Id = e.Id,
                    Date = e.Date,
                    IdBenhNhan = e.IdBenhNhan,
                    IdTrieuChung = e.IdTrieuChung,
                    GiaTri = e.GiaTri
                });
            return query;
        }
        public void Update(TrieuChungBanDauDto dto, int facilityId)
        {
            var entity = this._dal.GetAll().FirstOrDefault(e => e.Id == dto.Id && e.Id == facilityId);
            if (entity == null) throw new Exception("Đối tượng không tồn tại");
            //entity.Ten = dto.Ten;
            entity.GiaTri = dto.GiaTri;
            entity.IdBenhNhan = dto.IdBenhNhan;
            entity.IdTrieuChung = dto.IdTrieuChung;
            entity.Date = dto.Date;
            this.SaveChanges();
        }
        public void Insert(TrieuChungBanDauDto dto)
        {
            var entity = new bn_bn_TrieuChungBanDau();
            entity.Date = dto.Date;
            entity.IdBenhNhan = dto.IdBenhNhan;
            entity.IdTrieuChung = dto.IdTrieuChung;
            entity.GiaTri = dto.GiaTri;
            this._dal.Add(entity);
            this.SaveChanges();
            dto.Id = entity.Id;
        }
        public void Delete(int id, int facilityId)
        {
            var entity = this._dal.GetAll().FirstOrDefault(e => e.Id == id && e.Id == facilityId);
            if (entity == null) throw new Exception("Đối tượng không tồn tại");
            this._dal.Delete(entity);
            this.SaveChanges();
        }
        public void DeleteWhereHSBenhNhan(int ID)
        {
            this._dal.Delete(e => e.IdBenhNhan == ID);
            this.SaveChanges();
        }
    }
}
